/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.teaching.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.teaching.entity.Answer;
import com.thinkgem.jeesite.modules.teaching.service.AnswerService;

/**
 * 管理题库下的每个题目情况Controller
 * @author 罗洪州
 * @version 2018-01-28
 */
@Controller
@RequestMapping(value = "${adminPath}/teaching/answer")
public class AnswerController extends BaseController {

	@Autowired
	private AnswerService answerService;
	
	@ModelAttribute
	public Answer get(@RequestParam(required=false) String id) {
		Answer entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = answerService.get(id);
		}
		if (entity == null){
			entity = new Answer();
		}
		return entity;
	}
	
	@RequiresPermissions("teaching:answer:view")
	@RequestMapping(value = {"list", ""})
	public String list(Answer answer, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Answer> page = answerService.findPage(new Page<Answer>(request, response), answer); 
		model.addAttribute("page", page);
		return "modules/teaching/answerList";
	}

	@RequiresPermissions("teaching:answer:view")
	@RequestMapping(value = "form")
	public String form(Answer answer, Model model) {
		model.addAttribute("answer", answer);
		return "modules/teaching/answerForm";
	}

	@RequiresPermissions("teaching:answer:edit")
	@RequestMapping(value = "save")
	public String save(Answer answer, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, answer)){
			return form(answer, model);
		}
		answerService.save(answer);
		addMessage(redirectAttributes, "保存题目管理成功");
		return "redirect:"+Global.getAdminPath()+"/teaching/answer/?repage";
	}
	
	@RequiresPermissions("teaching:answer:edit")
	@RequestMapping(value = "delete")
	public String delete(Answer answer, RedirectAttributes redirectAttributes) {
		answerService.delete(answer);
		addMessage(redirectAttributes, "删除题目管理成功");
		return "redirect:"+Global.getAdminPath()+"/teaching/answer/?repage";
	}

}